package org.smart.access.utils;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Primary
@Component
public class IdGeneratorImpl implements IdentifierGenerator, IdGeneratorFactory {

    @Value("${snowflake.datacenter:0}")
    private Long workerId = 0L;
    @Value("${snowflake.worker:0}")
    private Long datacenterId = 0L;

    private final SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(workerId, datacenterId);

    @Override
    public boolean assignId(Object idValue) {
//        return idValue?.let { it as Long == 0L } ?: true
        if (idValue == null) {
            return true;
        }
        if (idValue instanceof Long) {
            return (Long) idValue == 0L;
        }
        return true;
    }

    /**
     * Deprecated：  由于此项目不使用UUID，此方法暂时不实现
     */
    @Deprecated
    @Override
    public String nextUUID(Object entity) {
        return IdentifierGenerator.super.nextUUID(entity);
    }

    @Override
    public Number nextId(Object entity) {
        return snowflakeIdWorker.nextId();
    }

    @Override
    public Long nextId() {
        return snowflakeIdWorker.nextId();
    }
}
